<?php
/**
 * Drop class, implement the drop action to drop something on floor
 * @package action
 * @author alcaitiff
 */
class Drop implements Action{

    /**
     * @static
     * @var integer
     **/
	static private $tick;
    /**
     * @static
     * @var Char
     **/
	static private $char;
    /**
    * Usage: drop [number|"all"] Item [number] [container [number]]
	*
    * The container may be on hands or floor, if container parameter is not present the item must be on your hands
	* @static
    * @param Command $command Command
    * @param int $tick Tick
    * @return String This command will return a msg
    */
	static public function run($command,$tick){
		self::$tick=$tick;
		self::$char=$command->character;
        $parameters=$command->getArrangedParameters(1,5,true);
		if($parameters==NULL){
            return 'Invalid Parameters';
        }else{
            return self::dropItem($parameters[0], $parameters[1], $parameters[2], (isset($parameters[3]))?$parameters[3]:null, (isset($parameters[4]))?$parameters[4]:null);
        }
	}
    /**
     * Drop an Item
     * @param int|string $num
     * @param string $name
     * @param int $index
     * @param string $containerName
     * @param int $containerIndex
     * @return string
     */
    static private function dropItem($num,$name,$index,$containerName=null,$containerIndex=1){
        $item=null;
        $container=null;
        if($containerName!==null){
            //get container
            $container=self::$char->getItem($containerName, $containerIndex);
            if($container!==NULL){
                if($container->container!=null){
                    //get item from container
                    $item=$container->container->getItemByName($name,$index);
                    if($item!==null){
                        return self::$char->dropItemFromContainer($item,$num,$container);
                    }
                }
            }
        }else{
            //drop from hands
            return self::$char->dropItemFromHands($num,$name,$index);
        }

        
    }

}